<?php

require_once dirname(__FILE__).'/../lib/sony_assetsGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/sony_assetsGeneratorHelper.class.php';

/**
 * sony_assets actions.
 *
 * @package    astolfo
 * @subpackage sony_assets
 * @author     Emerson Estrella
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sony_assetsActions extends autoSony_assetsActions
{
	public function executeIndex(sfWebRequest $request)
	{
		
		//$this->setLayout("layout_min");
		
		//echo ">>>".$request->getParameter('category_id');
		
		$ids = Doctrine_Query::create()
			->select('ca.sony_asset_id')
			->from('SonyCategoryAsset ca')
			->where('ca.sony_asset_id > 0')
			->execute();

		$this->query = Doctrine_Query::create()
			->select('a.*')
			->from('SonyAsset a')
			->whereNotIn('a.id', $ids)
			->execute();
		
		$this->filters = $this->configuration->getFilterForm($this->getFilters());
		
		if($request->getParameter('category_id')>0)
			$this->category = Doctrine::getTable('SonyCategory')->find($request->getParameter('category_id'));
		
    // sorting
    if ($request->getParameter('sort') && $this->isValidSortColumn($request->getParameter('sort')))
    {
      $this->setSort(array($request->getParameter('sort'), $request->getParameter('sort_type')));
    }

    // pager
    if ($request->getParameter('page'))
    {
      $this->setPage($request->getParameter('page'));
    }

    $this->pager = $this->getPager();
    $this->sort = $this->getSort();
	}

	protected function getPager()
	{
		$pager = $this->configuration->getPager('SonyAsset');
		$pager->setQuery($this->query);
		$pager->setPage($this->getPage());
		$pager->init();
		return $pager;
	}

	public function executeBatchSave(sfWebRequest $request){
		//echo ">>>".$request->getParameter('category_id');
		$ids = $request->getParameter('ids');
		foreach($ids as $id){
			//$sectionAssets = Doctrine::getTable('SonyCategoryAsset')->findBySonyCategoryIdAndSonyAssetId($request->getParameter('category_id'), $id);
			//if(!$sectionAssets)
			$sectionAsset = new SonyCategoryAsset();
			$sectionAsset->setSonyCategoryId($request->getParameter('category_id'));
			$sectionAsset->setSonyAssetId($id);
			$sectionAsset->setIsActive(1);
			$sectionAsset->save();
		}
		die('<script>parent.updateCategoryAssets('.$request->getParameter('category_id').');</script>');
	}
	
	
}
